﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Routing;

namespace Api.Hub.Constraints
{
    public class CultureRouteConstraint : IHttpRouteConstraint
    {
        private static List<string> cultureInfos = null;

        static CultureRouteConstraint()
        {
            cultureInfos = CultureInfo.GetCultures(CultureTypes.AllCultures).Select(c => c.Name).ToList();
        }

        public bool Match(System.Net.Http.HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary<string, object> values, HttpRouteDirection routeDirection)
        {
            object culture = null;
            if (values.TryGetValue(parameterName, out culture))
            {
                return cultureInfos.Any(c => c.ToLower() == culture.ToString().ToLower());
            }

            return false;
        }
    }
}
